/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DlgConexion.java
 *
 * Created on 07/11/2011, 07:05:52 PM
 */
package poo.biblioteca.vistas;

import java.awt.event.ActionListener;

/**
 *
 * @author apo
 */
public class DlgConexion extends javax.swing.JDialog {

    /** Creates new form DlgConexion */
    public DlgConexion(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public void addBtnCancelarActionListener (ActionListener al) {
        
        btnCancelar.addActionListener(al);
    }
    
    public void addBtnAceptarActionListener (ActionListener al) {
        btnAceptar.addActionListener(al);
    }
    
    public void btnTestAddActionListener (ActionListener al) {
        btnTest.addActionListener(al);
    }
    
    public String getConnectionString () {
        return "jdbc:mysql://" + txtHost.getText() + ":" 
                + txtProtocol.getText() + "/"
                + txtBD.getText() + "?"
                + "user="+ txtUsuario.getText() + "&"
                + "password=" + password.getText();
    }
    
    public String getHost () {
        return txtHost.getText();
    }
    
    public String getProtocol() {
        return txtProtocol.getText();
    }
    
    public String getBDName (){
        return txtBD.getText();
    }
    
    public String getUsuario() {
        return txtUsuario.getText();
    }
    
    public String getPassword() {
        return password.getText();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtHost = new javax.swing.JTextField();
        txtBD = new javax.swing.JTextField();
        password = new javax.swing.JPasswordField();
        txtUsuario = new javax.swing.JTextField();
        lblHost = new javax.swing.JLabel();
        lblBD = new javax.swing.JLabel();
        lblUsuario = new javax.swing.JLabel();
        lblPassword = new javax.swing.JLabel();
        btnAceptar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        txtProtocol = new javax.swing.JTextField();
        lblProtocolo = new javax.swing.JLabel();
        lblConectividad = new javax.swing.JLabel();
        lblConectividadResultado = new javax.swing.JLabel();
        btnTest = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Configurar Conexión a Base de Datos");
        setModal(true);
        setResizable(false);

        txtHost.setText("localhost");

        txtBD.setText("biblioteca");

        txtUsuario.setText("usuario de bd");

        lblHost.setText("Host:");

        lblBD.setText("Base de Datos:");

        lblUsuario.setText("Usuario:");

        lblPassword.setText("Password:");

        btnAceptar.setText("Aceptar");

        btnCancelar.setText("Cancelar");

        lblProtocolo.setText("Protocol:");

        lblConectividad.setText("Conectividad:");

        btnTest.setText("Test");

        btnSalir.setText("Salir de la Aplicación");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblHost)
                            .addComponent(lblProtocolo)
                            .addComponent(lblBD)
                            .addComponent(lblUsuario)
                            .addComponent(lblPassword))
                        .addGap(13, 13, 13)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtUsuario, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
                            .addComponent(password, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
                            .addComponent(txtBD, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
                            .addComponent(txtHost, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
                            .addComponent(txtProtocol, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(lblConectividad)
                                .addGap(18, 18, 18)
                                .addComponent(lblConectividadResultado, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(btnSalir)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnCancelar)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btnAceptar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnTest, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblHost)
                    .addComponent(txtHost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtProtocol, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblProtocolo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtBD, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblBD))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblUsuario))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblPassword))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblConectividad)
                    .addComponent(btnTest)
                    .addComponent(lblConectividadResultado, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnCancelar)
                        .addComponent(btnSalir))
                    .addComponent(btnAceptar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JButton btnTest;
    private javax.swing.JLabel lblBD;
    private javax.swing.JLabel lblConectividad;
    private javax.swing.JLabel lblConectividadResultado;
    private javax.swing.JLabel lblHost;
    private javax.swing.JLabel lblPassword;
    private javax.swing.JLabel lblProtocolo;
    private javax.swing.JLabel lblUsuario;
    private javax.swing.JPasswordField password;
    private javax.swing.JTextField txtBD;
    private javax.swing.JTextField txtHost;
    private javax.swing.JTextField txtProtocol;
    private javax.swing.JTextField txtUsuario;
    // End of variables declaration//GEN-END:variables

    public void setValoresPorDefecto(String host, String protocol, String dbName, String user, String password) {
        txtHost.setText(host);
        txtProtocol.setText(protocol);
        txtBD.setText(dbName);
        txtUsuario.setText(user);
    }

    public void setResultadoTest(String message) {
        lblConectividadResultado.setText(message);
    }

    public void btnSalirAddActionListener(ActionListener actionListener) {
        btnSalir.addActionListener(actionListener);
    }
}
